package com.jinke.api.modules.third.ec.util;

import com.jinke.api.common.util.CommonUtil;
import com.jinke.api.modules.third.ec.model.Box;
import com.jinke.api.modules.third.ec.model.Ec;
import com.jinke.api.modules.third.ec.model.Good;
import com.jinke.api.modules.third.ezeeship.util.EzeeShipUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author huang
 * @data 2024-04-29
 */
@Slf4j
public class EcUtils {
    private static final String API_TOKEN = "db7be10538266cb15c819a2cef762917";

    /**
     * 发送post请求
     */
    public static String sendPostRequest(String url, String requestBody) throws IOException {
        String seq = CommonUtil.getOrderNo();
        log.info("send :  {}, {}", seq, requestBody);
        HttpURLConnection connection = null;
        try {
            // 创建连接
            // 创建连接
            URL apiUrl = new URL(url);
            if ("true".equals(System.getenv("local"))) {
                Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", 7890));
                connection = (HttpURLConnection) apiUrl.openConnection(proxy);
            } else {
                connection = (HttpURLConnection) apiUrl.openConnection();
            }

            connection = (HttpURLConnection) apiUrl.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("apiToken", API_TOKEN);
            connection.setDoOutput(true);

            // 写入请求体
            try (OutputStream outputStream = connection.getOutputStream()) {
                outputStream.write(requestBody.getBytes("UTF-8"));
            }
            int responseCode = connection.getResponseCode();

            if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST) {
                // 获取错误消息体
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()))) {
                    StringBuilder errorResponse = new StringBuilder();
                    String line;
                    while ((line = reader.readLine()) != null) {
                        errorResponse.append(line);
                    }
                    log.error("Error response :  {}, {}", seq, errorResponse);
                    return errorResponse.toString();
                }
            } else {
                // 获取正常响应
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
                    StringBuilder response = new StringBuilder();
                    String line;
                    while ((line = reader.readLine()) != null) {
                        response.append(line);
                    }
                    log.info("rec :  {}, {}", seq, response);
                    return response.toString();
                }
            }
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
    }

    /**
     * 下单
     */
    public static void createOrder() {

    }

    /**
     * 取消订单
     */
    public static void cancelOrder() {

    }


    public static void main(String[] args) {
        //组装数据
        //发送请求
        String url = "https://api.saas.eccangtms.com/open-api/order/calculate";
        String body = assemble();
        try {
            String resr = sendPostRequest(url, body);
            System.out.println(resr);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //更改订单状态
    }

    private static String assemble() {
        Ec ec = new Ec();
        List<Box> boxList = new ArrayList<>();
        List<Good> goodList = new ArrayList<>();
        ec.setApiToken("8eb0a23f5c36794b804bf24b243d7c55");
        ec.setProductCode("UPS Ground专线");
        ec.setCustomerNo("TEST20210002");
        ec.setGoodsType(3);
        ec.setOrderWeight(15);
        ec.setCurrencyCode("USD");
        ec.setSignatureService("NO");
        return null;
    }

}
